package com.example.esdemo.controllers;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.example.esdemo.forms.PostCreateForm;
import com.example.esdemo.forms.PostDeleteForm;
import com.example.esdemo.forms.PostSearchForm;
import com.example.esdemo.services.IPostService;
import com.example.esdemo.vo.PostVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Cary
 * @date 2022/6/21
 */
@RestController
@RequestMapping("/post")
public class PostController {
    @Autowired
    private IPostService postService;

    @RequestMapping("/create")
    public R<String> create(@RequestBody @Valid PostCreateForm form) {
        postService.create(form);
        return R.ok("创建文章成功");
    }

    @RequestMapping("/delete")
    public R<String> create(@RequestBody @Valid PostDeleteForm form) {
        postService.delete(form);
        return R.ok("删除文章成功");
    }

    @RequestMapping("/dbList")
    public R<List<PostVo>> dbList() {
        List<PostVo> list = postService.dbList();
        return R.ok(list);
    }

    @RequestMapping("/esList")
    public R<List<PostVo>> esList() {
        List<PostVo> list = postService.esList();
        return R.ok(list);
    }

    @RequestMapping("/search")
    public R<IPage<PostVo>> search(@RequestBody @Valid PostSearchForm form) {
        IPage<PostVo> page = postService.search(form);
        return R.ok(page);
    }
}
